var Events:any = {
    subscribers: {
        'any': []
    },
    cache: {

    },
    //添加订阅
    subscribe: function (type = 'any', fn:any) {
        if (!this.subscribers[type]) {
            this.subscribers[type] = [];
        }
        this.subscribers[type].push(fn); //将订阅方法保存在数组里
        if(this.cache[type] && this.cache[type] instanceof Array){
            this.cache[type].forEach((args:any)=>{
                fn(...args)
            })
            this.cache[type] = null;
        }
    },
    //退订
    unsubscribe: function (type = 'any', fn:any) {
        if(this.subscribers[type]){
            this.subscribers[type].filter(function (item:any) {
                return item !== fn;
            }); //将退订的方法从数组中移除
        }
    },
    //发布订阅
    publish: function (type = 'any', ...args: any) {
        if(this.subscribers && this.subscribers[type]){
            this.subscribers[type].forEach(function (item:any) {
                item(...args);	//根据不同的类型调用相应的方法
            });
        }else{
            console.log(args,type)
            if(!this.cache[type]){
                this.cache[type] = [];
            }
            this.cache[type].push(args);
        }
    }
};

export default Events;
